package ru.hh.team02.chat.server.SendMessage;

import ru.hh.team02.chat.server.Client;
import ru.hh.team02.chat.server.SendMessage.sendingBlockingQueue.SendingBlockingQueue;

public class SendingService {
	
	static private SendingBlockingQueue sendingQueue = new SendingBlockingQueue(100);

	/**
	 * Put the message for sending in the queue.
	 * The addressee is already encapsulated inside the message.
	 * @param message
	 */
	public static void sendMessage(Message message) {
		sendingQueue.add(message);
	}

	/**
	 * Create a message to a particular recipient and put it into the sending queue.
	 * @param text Message text
	 * @param recepient Recipient the message is addressed to.
	 */
	public static void sendMessage(String text, Client recepient) {
		Message message = new Message(text, recepient);
		sendMessage(message);
	}
	
	/**
	 * Create a bunch of messages to all the clients of the room and
	 * put the messages in the queue for sending to them.
	 * This method is just for optimization, as we may here once block the queue and
	 * add all the messages instead of blocking and unblocking the queue for each message.
	 * @param text Message text
	 * @param room Room to send the message to
	 */
	public static void sendMessageToRoom(String text, String room) {
		sendingQueue.add(text, room);
	}
	
	/**
	 * Terminate the queue: stop accepting new messages, send the messages
	 * lodging in the queue and stop all sending threads.
	 */
	public static void teminate() {
		sendingQueue.terminate();
	}
	
	

}
